package com.gjs.cd.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gjs.cd.mapper.CdCodeValidTermMapper;
import com.gjs.cd.pojo.dto.CdCodeValidTermReq;
import com.gjs.cd.pojo.entity.CdCodeValidTerm;
import com.gjs.cd.service.ICdCodeValidTermService;
import com.gjs.common.enums.entity.QuarterEnum;
import com.gjs.common.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhongwenguang
 * @date 2022-3-10 17:43
 */
@Service
public class CdCodeValidTermServiceImpl extends ServiceImpl<CdCodeValidTermMapper, CdCodeValidTerm> implements ICdCodeValidTermService {

    //查询同一年和同一季度是否存在，type为0是新增，type为1是更新检查，
    private CdCodeValidTerm checkTerm(CdCodeValidTermReq cdCodeValidTermReq,int type){
        Integer year = cdCodeValidTermReq.getCodeYear();//获取年份
        QuarterEnum quater = cdCodeValidTermReq.getCodeQuarter();//获取季度

        //判断该年份该季度是否已经存在
        LambdaQueryWrapper<CdCodeValidTerm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(year!=0,CdCodeValidTerm::getCodeYear,year);
        lambdaQueryWrapper.eq(quater!=null,CdCodeValidTerm::getCodeQuarter,quater);
        if (type ==1){
            LocalDateTime expireTime = cdCodeValidTermReq.getExpireTime();
            lambdaQueryWrapper.eq(expireTime != null,CdCodeValidTerm::getExpireTime,expireTime);
        }
        return this.getOne(lambdaQueryWrapper);
    }

    //更新
    @Override
    public Boolean updateTerm(CdCodeValidTermReq cdCodeValidTermReq) {
        Boolean flag = false;
        CdCodeValidTerm ct = checkTerm(cdCodeValidTermReq,1);//参数
        if (ct != null){
            throw new BizException("当前更新数据已存在，不可更新为相同数据！");
        }
        try{
            flag = this.updateById(BeanUtil.copyProperties(cdCodeValidTermReq,CdCodeValidTerm.class));
        }catch (Exception e){
            throw new BizException("数据更新失败！");
        }
        return flag;
    }

    //新增
    @Override
    public Boolean addTerm(CdCodeValidTermReq cdCodeValidTermReq) {
        Boolean flag = false;
        CdCodeValidTerm ct = checkTerm(cdCodeValidTermReq,0);
        if(ct != null){
            throw new BizException("当前新增数据已存在，不可添加相同数据！");
        }
        try{
            flag = this.save(BeanUtil.copyProperties(cdCodeValidTermReq,CdCodeValidTerm.class));
        }catch (Exception e){
            throw new BizException("数据新增失败！");
        }
        return flag;
    }

    @Override
    public List<CdCodeValidTerm> getListYear() {

        LambdaQueryWrapper<CdCodeValidTerm> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.select(CdCodeValidTerm::getId,CdCodeValidTerm::getCodeYear,CdCodeValidTerm::getCodeQuarter);
        List<CdCodeValidTerm> list = this.list(lambdaQueryWrapper);
        return list;
    }

    @Override
    public Wrapper<CdCodeValidTerm> buildWrapper(CdCodeValidTermReq req) {
        LambdaQueryWrapper<CdCodeValidTerm> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(req.getCodeYear() != 0,CdCodeValidTerm::getCodeYear,req.getCodeYear());
        return wrapper;
    }
}
